<?php  
namespace app\api\controller;
use think\Db;
use think\Controller;
use think\Cache;
/**
 * 上传图片接口
 */
class ApiUploadController extends Controller
{
	/**
	 * layui上传图片接口
	 */
	public function layuiUploadFile()
	{
		//if(!$this->request->isPost()){return false;}
		//文件保存文件夹
        $data=$_FILES;//获取文件
        $exts=pathinfo($data['file']['name']);
        /*if(!isset($exts['extension'])){
            return  zy_json(false,'图片格式不合法！');
        }
        $format=['jpeg','gif','bmp','png','jpg'];//文件格式
        if(!in_array($exts['extension'],$format)){
            return  zy_json(false,'请上传图片格式文件！');
        }*/
        //1M=1048576B  最大10M
        $size=$data['file']['size'];
        if((1024*1024*10)<$size){
            return  $this->layuiUploadReturnJson(1,'图片大小不超过10MB,请检查!');
        }
        //临时文件
        $tmp=$data['file']['tmp_name'];
        //存储位置 upload
        $fileName='upload/layuiUploadFile/'.time().'.'.$exts['extension'];
        if(!move_uploaded_file($tmp,$fileName)){
            //return  zy_json(false,'上传失败，请稍后再试！');
            return  $this->layuiUploadReturnJson(1,'上传失败，请稍后再试！');
        }else{
        	return  $this->layuiUploadReturnJson(0,'上传成功!','/'.$fileName);
            //return zy_json(true,'上传成功',$fileName);
        }
	}

	/**
	 * layui 编辑器返回数据
	 */
	private function layuiUploadReturnJson($code = 0,$msg = '',$src='',$title='img')
	{
		$data = [
			'code' => $code,
			'msg' =>	$msg,
			'data' =>[
				'src' => $src,
				'title' => $title
			]
		];
		return json_encode($data,JSON_UNESCAPED_UNICODE);
	}

    //公共上传文件路劲 弃用20191012
    public function uploadFiles()
    {
        //if(!$this->request->isPost()){return false;}
        //文件保存文件夹
        $data=$_FILES;//获取文件
        if(empty($data)){
            return  zy_json_echo(false,'文件上传失败，未找到文件！',null,101);
        }
        $exts=pathinfo($data['file']['name']);
        /*if(!isset($exts['extension'])){
            return  zy_json(false,'图片格式不合法！');
        }
        $format=['jpeg','gif','bmp','png','jpg'];//文件格式
        if(!in_array($exts['extension'],$format)){
            return  zy_json(false,'请上传图片格式文件！');
        }*/
        //1M=1048576B  最大10M
        $size=$data['file']['size'];
        if((1024*1024*10)<$size){
            return  zy_json_echo(false,'图片大小不超过10MB,请检查!',null,102);
        }
        //临时文件
        $tmp=$data['file']['tmp_name'];
        //存储位置 upload
        $fileName='upload/uploadFile/'.date('YmdHi',time()).time().'.'.$exts['extension'];
        if(!move_uploaded_file($tmp,$fileName)){
            return  zy_json_echo(false,'上传失败，请稍后再试！',null,103);
        }else{
            return  zy_json_echo(true,'上传成功!','/'.$fileName);
        }
    }

    /**
     * 公共上传文件方法
     * file  文件目录
     */
    public function uploadFile( $file = null , $type = [ 'format' => ['zip','rar'], 'error'=>'文件只支持zip/rar压缩文件，请检查。'] )
    {   
        $result = null;
        if( empty($file) ){
            return $this->resultInfo(false,'文件存储目录不能为空！');
        }
        $path = 'upload/'.$file;
        if( !file_exists( $path ) ){
            mkdir( $path , 0777 , true );
        }

        $data=$_FILES;//获取文件
        if(empty($data)){
            return  $this->resultInfo(false,'未获取到文件信息。');
        }
        $exts=pathinfo($data['file']['name']);
        if(!isset($exts['extension'])){
            return  $this->resultInfo(false,'文件格式不合法。');
        }
        $format= $type['format'];//文件格式
        if(!in_array($exts['extension'],$format)){
            return   $this->resultInfo( false , $type['error'] );
        }
        //1M=1048576B  最大10M
        $size=$data['file']['size'];
        if((1024*1024*50)<$size){
            return   $this->resultInfo(false,'文件大小不超过50MB,请检查!');
        }

        $timeMd5=md5(time());
        //产生随机数
        $start=rand(5,20);
        $length=rand(5,10);
        $timeStr=substr($timeMd5,$start,$length);

        //临时文件
        $tmp = $data['file']['tmp_name'];
        //存储位置 upload
        $fileName = $path.'/'.date('YmdHi',time()).time().$timeStr.'.'.$exts['extension'];
        if(!move_uploaded_file($tmp,$fileName)){
            return   $this->resultInfo(false,'数据上传失败，请稍后再试!');
        }

        return  $this->resultInfo( true, $fileName );
    }   

    /**
     * @param  string $error   [description]
     * @param  string $message [description]
     * @return [type]          [description]
     */
    private function resultInfo( $error= '' , $message = "" )
    {
        return [ 'error' => $error , 'message' => $message ];
    } 



}